package com.qingfeng.core.aop.exception;

import com.qingfeng.core.dao.ErrorDao;
import com.qingfeng.core.data.ResultData;
import com.qingfeng.core.entity.Error;
import com.qingfeng.core.utils.FastJsonConvertUtil;
import com.qingfeng.core.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.Map;

/**
 * @description: ControllerAdvice类
 * @author:LWY
 * @date: 2017/11/10 9:52
 * @modified:
 */
@RestControllerAdvice
public class ControllerAdvice {
    public static final Logger logger = LoggerFactory.getLogger(ControllerAdvice.class);

    @Autowired
    ErrorDao errorDao;
    @Value("${my.platform}")
    public Integer platform;

    @ExceptionHandler(value = Exception.class)
    public ResultData exception(Exception exception) {
        ResultData resultData = null;
        if (exception instanceof HttpException) {
            resultData = new ResultData(ResultData.FAIL, exception.getMessage(), "");
        } else {
            //系统异常，保存入库
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            String url = request.getRequestURL().toString();
            String method = request.getMethod();
            Map<String, String[]> parameterMap = request.getParameterMap();
            String parameters = FastJsonConvertUtil.convertObjectToJSON(parameterMap);
            //打印日志
            logger.info("请求url={}", url);
            logger.info("请求method={}", method);
            logger.info("参数={}", parameters);
            StringWriter sw = new StringWriter();
            exception.printStackTrace(new PrintWriter(sw));
            Throwable cause = exception.getCause();
            Error error = new Error();
            error.setId(UuidUtil.generatorUUID());
            error.setMessage(exception.getMessage());
            error.setPlatform(platform);
            error.setCreate_time(new Date());
            error.setUrl(url);
            error.setMethod(method);
            error.setParameters(parameters);
            if (cause != null) {
                error.setCause(cause.getMessage());
            }
            error.setStack_trace(sw.toString());
            errorDao.insert(error);
            exception.printStackTrace();
            resultData = new ResultData(ResultData.FAIL, "发生错误了,请把错误ID发送给客服人员,ID:" + error.getId(), "");
        }
        return resultData;
    }
}
